package lc.smart.android.main;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * 点击照相应用程序
 * @author lichong_951@163.com
 * */
public class MyCameraSimpleActivity extends Activity implements
		OnClickListener, SurfaceHolder.Callback, Camera.PictureCallback
{
	public static final String TAG_STRING = "MyCameraSimpleActivity";//标识
	private Context mContext;
	SurfaceView cameraView;
	SurfaceHolder surfaceHolder;
	Camera camera;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		mContext = this;
		setContentView(R.layout.activity_my_camera_simple);

		cameraView = (SurfaceView) findViewById(R.id.cameraView_my_camera_simple);
		surfaceHolder = cameraView.getHolder();
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceHolder.addCallback(this);

		cameraView.setFocusable(true);
		cameraView.setFocusableInTouchMode(true);
		cameraView.setClickable(true);

		cameraView.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_camera_simple, menu);
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height)
	{
		// TODO Auto-generated method stub
		camera.startPreview();

	}

	@SuppressLint("NewApi")
	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		camera = Camera.open();
		try
		{
			camera.setPreviewDisplay(surfaceHolder);
			Camera.Parameters parameters = camera.getParameters();
			if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
			{
				parameters.set("orientation", "portrait");
				//android 2.2和以上版本
				camera.setDisplayOrientation(90);
				//android 2.0和以上版本
				parameters.setRotation(90);
			}
			List<String> colorEffects = parameters.getSupportedColorEffects();
			Iterator<String> cei = colorEffects.iterator();
			while (cei.hasNext())
			{
				String currentEffectString = cei.next();
				if (currentEffectString
						.equals(Camera.Parameters.EFFECT_SOLARIZE))
				{
					parameters
							.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);
					break;
				}
				//结束android 2.0和更高版本的效果
				camera.setParameters(parameters);

			}
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			camera.release();
		}

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{

		camera.stopPreview();
		camera.release();

	}//结束活动

	@Override
	public void onClick(View v)
	{
		camera.takePicture(null, null, null);

	}

	@Override
	public void onPictureTaken(byte[] data, Camera camera)
	{
		Uri imageFileUri = getContentResolver().insert(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new ContentValues());
		try
		{
			OutputStream imageFileOutputStream = getContentResolver()
					.openOutputStream(imageFileUri);
			imageFileOutputStream.write(data);
			imageFileOutputStream.flush();
			imageFileOutputStream.close();
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();

			showMessage(e.getMessage());

		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			showMessage(e.getMessage());
		}
		camera.startPreview();

	}

	public void showMessage(String msg)
	{
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
	}

}
